﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.XPath;
using System.Xml;

namespace FTI.SP.LADM.EnhancedContentQueryWebPart.Exslt
{
    public sealed class ExsltMath
    {
        // Fields
        private static ExsltMath _Instance;

        // Methods
        private ExsltMath()
        {
        }

        public static double Abs(double number)
        {
            return Math.Abs(number);
        }

        public static double Acos(double number)
        {
            return Math.Acos(number);
        }

        public static double Asin(double number)
        {
            return Math.Asin(number);
        }

        public static double Atan(double number)
        {
            return Math.Atan(number);
        }

        public static double Atan2(double x, double y)
        {
            return Math.Atan2(x, y);
        }

        public static double Avg(XPathNodeIterator iterator)
        {
            double num = 0.0;
            int count = iterator.Count;
            if (count == 0)
            {
                return double.NaN;
            }
            try
            {
                while (iterator.MoveNext())
                {
                    num += XmlConvert.ToDouble(iterator.Current.Value);
                }
            }
            catch (FormatException)
            {
                return double.NaN;
            }
            return (num / ((double)count));
        }

        public static double Constant(string constantName)
        {
            switch (constantName)
            {
                case "E":
                    return 2.7182818284590451;

                case "PI":
                    return 3.1415926535897931;
            }
            return double.NaN;
        }

        public static double Cos(double number)
        {
            return Math.Cos(number);
        }

        public static double Exp(double x)
        {
            return Math.Exp(x);
        }

        public static XPathNodeIterator Highest(XPathNodeIterator iterator)
        {
            ExsltNodeList list = new ExsltNodeList();
            if (iterator.Count != 0)
            {
                try
                {
                    iterator.MoveNext();
                    double num = XmlConvert.ToDouble(iterator.Current.Value);
                    list.Add(iterator.Current.Clone());
                    while (iterator.MoveNext())
                    {
                        double num2 = XmlConvert.ToDouble(iterator.Current.Value);
                        if (num2 > num)
                        {
                            num = num2;
                            list.Clear();
                            list.Add(iterator.Current.Clone());
                        }
                        else if (num2 == num)
                        {
                            list.Add(iterator.Current.Clone());
                        }
                    }
                }
                catch
                {
                    list.Clear();
                    return ExsltCommon.ExsltNodeListToXPathNodeIterator(list);
                }
            }
            return ExsltCommon.ExsltNodeListToXPathNodeIterator(list);
        }

        public static double Log(double x)
        {
            return Math.Log(x);
        }

        public static XPathNodeIterator Lowest(XPathNodeIterator iterator)
        {
            ExsltNodeList list = new ExsltNodeList();
            if (iterator.Count != 0)
            {
                try
                {
                    iterator.MoveNext();
                    double num = XmlConvert.ToDouble(iterator.Current.Value);
                    list.Add(iterator.Current.Clone());
                    while (iterator.MoveNext())
                    {
                        double num2 = XmlConvert.ToDouble(iterator.Current.Value);
                        if (num2 < num)
                        {
                            num = num2;
                            list.Clear();
                            list.Add(iterator.Current.Clone());
                        }
                        else if (num2 == num)
                        {
                            list.Add(iterator.Current.Clone());
                        }
                    }
                }
                catch
                {
                    list.Clear();
                    return ExsltCommon.ExsltNodeListToXPathNodeIterator(list);
                }
            }
            return ExsltCommon.ExsltNodeListToXPathNodeIterator(list);
        }

        public static double Max(XPathNodeIterator iterator)
        {
            double num;
            if (iterator.Count == 0)
            {
                return double.NaN;
            }
            try
            {
                iterator.MoveNext();
                num = XmlConvert.ToDouble(iterator.Current.Value);
                while (iterator.MoveNext())
                {
                    double num2 = XmlConvert.ToDouble(iterator.Current.Value);
                    num = (num2 > num) ? num2 : num;
                }
            }
            catch
            {
                return double.NaN;
            }
            return num;
        }

        public static double Min(XPathNodeIterator iterator)
        {
            double num;
            if (iterator.Count == 0)
            {
                return double.NaN;
            }
            try
            {
                iterator.MoveNext();
                num = XmlConvert.ToDouble(iterator.Current.Value);
                while (iterator.MoveNext())
                {
                    double num2 = XmlConvert.ToDouble(iterator.Current.Value);
                    num = (num2 < num) ? num2 : num;
                }
            }
            catch
            {
                return double.NaN;
            }
            return num;
        }

        public static double Power(double x, double y)
        {
            return Math.Pow(x, y);
        }

        public static double Random()
        {
            Random random = new Random((int)DateTime.Now.Ticks);
            return random.NextDouble();
        }

        public static double Sin(double number)
        {
            return Math.Sin(number);
        }

        public static double Sqrt(double number)
        {
            return Math.Sqrt(number);
        }

        public static double Tan(double number)
        {
            return Math.Tan(number);
        }

        // Properties
        public static ExsltMath Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new ExsltMath();
                }
                return _Instance;
            }
        }

    }
}
